﻿using BCService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BCTMSRESTful.Configs
{
    public static class ArgumentExtension
    {
        /// <summary>
        /// 将字符串转换为一个数组参数。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <returns></returns>
        public static T[] AsArrayArgument<T>(this string str)
        {
            if (str == null)
            {
                return null;
            }

            if (str.ToUpperInvariant() == "@ALL")
            {
                return null;
            }

            if (str.Trim() == string.Empty)
            {
                return new T[0];
            }

            if (str.ToUpperInvariant() == "@ME")
            {
                //long? userId = applicationContextService?.Admin.AdminId ?? applicationContextService?.User.UserId;

                //if (userId == null)
                //{
                //    throw new ArgumentException($"未登录情况下 {str} 参数不适用。");
                //}

                //return new T[1] { (T)Convert.ChangeType(userId.Value, typeof(T)) };
            }

            return str.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries).Select(p => (T)Convert.ChangeType(p, typeof(T))).ToArray();
        }
    }
}
